import { defineEventHandler, getQuery } from 'h3';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);

  // 能力实例数据
  const capabilityInstances = [
    {
      id: '1',
      name: '客房管理能力',
      code: 'ROOM_MANAGEMENT',
      description: '提供客房状态管理、房态更新、房间分配等核心能力',
      type: 'business',
      category: 'hotel',
      resourceType: 'entity',
      capabilityType: 'workflow',
      status: 'enabled',
      priority: 95,
      version: '1.0.0',
      icon: 'ant-design:home-outlined',
      color: '#52c41a',
      tags: ['客房管理', '房态管理'],
      implementation: {
        type: 'service',
        serviceName: 'roomManagementService',
        methodName: 'updateRoomStatus',
        parameters: {
          statusTypes: [
            'vacant',
            'occupied',
            'reserved',
            'maintenance',
            'dirty',
          ],
          enableRealTimeSync: true,
          autoStatusTransition: true,
        },
      },
      binding: {
        resourceId: '1',
        resourceName: '豪华酒店主楼',
        bindingTime: '2024-01-10T09:00:00Z',
        bindingUser: 'admin',
      },
      metrics: {
        totalExecutions: 1250,
        successRate: 98.5,
        averageResponseTime: 120,
        lastExecution: '2024-01-15T16:30:00Z',
      },
      createdAt: '2024-01-01T00:00:00Z',
      updatedAt: '2024-01-15T14:20:00Z',
    },
    {
      id: '2',
      name: '预订管理能力',
      code: 'BOOKING_MANAGEMENT',
      description: '提供客房预订、订单处理、预订确认等业务能力',
      type: 'business',
      category: 'hotel',
      resourceType: 'service',
      capabilityType: 'workflow',
      status: 'enabled',
      priority: 90,
      version: '1.0.0',
      icon: 'ant-design:calendar-outlined',
      color: '#1890ff',
      tags: ['预订管理', '订单处理'],
      implementation: {
        type: 'service',
        serviceName: 'bookingManagementService',
        methodName: 'processBooking',
        parameters: {
          bookingTypes: ['individual', 'group', 'corporate'],
          paymentMethods: ['cash', 'card', 'online', 'voucher'],
          enableOverbooking: true,
          maxOverbookingRate: 0.1,
        },
      },
      binding: {
        resourceId: '1',
        resourceName: '豪华酒店主楼',
        bindingTime: '2024-01-10T09:00:00Z',
        bindingUser: 'admin',
      },
      metrics: {
        totalExecutions: 890,
        successRate: 99.2,
        averageResponseTime: 85,
        lastExecution: '2024-01-15T17:15:00Z',
      },
      createdAt: '2024-01-01T00:00:00Z',
      updatedAt: '2024-01-14T11:30:00Z',
    },
    {
      id: '3',
      name: '入住退房能力',
      code: 'CHECKIN_CHECKOUT',
      description: '提供客人入住登记、退房结算、房卡管理等能力',
      type: 'business',
      category: 'hotel',
      resourceType: 'entity',
      capabilityType: 'workflow',
      status: 'enabled',
      priority: 85,
      version: '1.0.0',
      icon: 'ant-design:user-add-outlined',
      color: '#722ed1',
      tags: ['入住管理', '退房管理'],
      implementation: {
        type: 'workflow',
        processDefinitionId: 'checkin_checkout_process',
        version: '1.0.0',
        parameters: {
          enableAutoCheckout: true,
          lateCheckoutAllowed: true,
          maxLateCheckoutHours: 2,
          enableExpressCheckout: true,
        },
      },
      binding: {
        resourceId: '3',
        resourceName: '行政套房1501',
        bindingTime: '2024-01-12T10:30:00Z',
        bindingUser: 'admin',
      },
      metrics: {
        totalExecutions: 156,
        successRate: 97.8,
        averageResponseTime: 45,
        lastExecution: '2024-01-15T18:45:00Z',
      },
      createdAt: '2024-01-01T00:00:00Z',
      updatedAt: '2024-01-13T16:20:00Z',
    },
    {
      id: '4',
      name: '定价引擎能力',
      code: 'PRICING_ENGINE',
      description: '提供动态定价、价格策略、优惠计算等能力',
      type: 'business',
      category: 'hotel',
      resourceType: 'service',
      capabilityType: 'rule',
      status: 'enabled',
      priority: 80,
      version: '1.0.0',
      icon: 'ant-design:dollar-outlined',
      color: '#fa8c16',
      tags: ['定价策略', '价格管理'],
      implementation: {
        type: 'rule',
        ruleSetName: 'hotel_pricing_rules',
        engine: 'drools',
        parameters: {
          pricingFactors: ['season', 'occupancy', 'competition', 'demand'],
          enableDynamicPricing: true,
          updateFrequency: 'hourly',
          enableAIBasedPricing: true,
        },
      },
      binding: {
        resourceId: '1',
        resourceName: '豪华酒店主楼',
        bindingTime: '2024-01-11T14:15:00Z',
        bindingUser: 'admin',
      },
      metrics: {
        totalExecutions: 2340,
        successRate: 99.8,
        averageResponseTime: 25,
        lastExecution: '2024-01-15T19:20:00Z',
      },
      createdAt: '2024-01-01T00:00:00Z',
      updatedAt: '2024-01-12T09:45:00Z',
    },
    {
      id: '5',
      name: '客人服务能力',
      code: 'GUEST_SERVICE',
      description: '提供客人服务请求、客房服务、投诉处理等能力',
      type: 'business',
      category: 'hotel',
      resourceType: 'service',
      capabilityType: 'workflow',
      status: 'enabled',
      priority: 75,
      version: '1.0.0',
      icon: 'ant-design:customer-service-outlined',
      color: '#eb2f96',
      tags: ['客人服务', '投诉处理'],
      implementation: {
        type: 'service',
        serviceName: 'guestServiceService',
        methodName: 'handleServiceRequest',
        parameters: {
          serviceTypes: [
            'room_service',
            'housekeeping',
            'maintenance',
            'concierge',
          ],
          priorityLevels: ['low', 'medium', 'high', 'urgent'],
          enableAutoAssignment: true,
        },
      },
      binding: {
        resourceId: '2',
        resourceName: '15楼',
        bindingTime: '2024-01-13T08:45:00Z',
        bindingUser: 'admin',
      },
      metrics: {
        totalExecutions: 567,
        successRate: 96.3,
        averageResponseTime: 180,
        lastExecution: '2024-01-15T20:10:00Z',
      },
      createdAt: '2024-01-01T00:00:00Z',
      updatedAt: '2024-01-14T13:25:00Z',
    },
    {
      id: '6',
      name: 'IoT设备管理能力',
      code: 'IOT_DEVICE_MANAGEMENT',
      description: '提供智能门锁、温控器、照明等IoT设备管理能力',
      type: 'technical',
      category: 'hotel',
      resourceType: 'capability',
      capabilityType: 'workflow',
      status: 'enabled',
      priority: 70,
      version: '1.0.0',
      icon: 'ant-design:setting-outlined',
      color: '#722ed1',
      tags: ['IoT设备', '智能控制'],
      implementation: {
        type: 'service',
        serviceName: 'iotDeviceService',
        methodName: 'controlDevice',
        parameters: {
          deviceTypes: [
            'smart_lock',
            'thermostat',
            'lighting',
            'tv',
            'curtain',
          ],
          enableRemoteControl: true,
          enableAutomation: true,
        },
      },
      binding: {
        resourceId: '3',
        resourceName: '行政套房1501',
        bindingTime: '2024-01-14T11:20:00Z',
        bindingUser: 'admin',
      },
      metrics: {
        totalExecutions: 890,
        successRate: 98.9,
        averageResponseTime: 65,
        lastExecution: '2024-01-15T21:05:00Z',
      },
      createdAt: '2024-01-01T00:00:00Z',
      updatedAt: '2024-01-15T10:15:00Z',
    },
    {
      id: '7',
      name: '数据分析能力',
      code: 'DATA_ANALYTICS',
      description: '提供酒店运营数据分析和商业智能能力',
      type: 'technical',
      category: 'hotel',
      resourceType: 'capability',
      capabilityType: 'report',
      status: 'enabled',
      priority: 65,
      version: '1.0.0',
      icon: 'ant-design:bar-chart-outlined',
      color: '#eb2f96',
      tags: ['数据分析', '商业智能'],
      implementation: {
        type: 'service',
        serviceName: 'analyticsService',
        methodName: 'analyzeData',
        parameters: {
          analysisTypes: [
            'occupancy',
            'revenue',
            'guest_satisfaction',
            'operational_efficiency',
          ],
          enablePredictiveAnalytics: true,
          enableRealTimeDashboards: true,
        },
      },
      binding: {
        resourceId: '10',
        resourceName: '酒店管理系统',
        bindingTime: '2024-01-09T16:30:00Z',
        bindingUser: 'admin',
      },
      metrics: {
        totalExecutions: 234,
        successRate: 99.5,
        averageResponseTime: 320,
        lastExecution: '2024-01-15T22:30:00Z',
      },
      createdAt: '2024-01-01T00:00:00Z',
      updatedAt: '2024-01-11T14:50:00Z',
    },
    {
      id: '8',
      name: '安全监控能力',
      code: 'SECURITY_MONITORING',
      description: '提供酒店安全监控、门禁管理、访客登记等能力',
      type: 'technical',
      category: 'hotel',
      resourceType: 'capability',
      capabilityType: 'workflow',
      status: 'enabled',
      priority: 80,
      version: '1.0.0',
      icon: 'ant-design:safety-outlined',
      color: '#ff4d4f',
      tags: ['安全监控', '门禁管理'],
      implementation: {
        type: 'service',
        serviceName: 'securityService',
        methodName: 'monitorSecurity',
        parameters: {
          monitoringTypes: [
            'cctv',
            'access_control',
            'fire_alarm',
            'emergency_exit',
          ],
          enableFacialRecognition: true,
          enableVisitorManagement: true,
        },
      },
      binding: {
        resourceId: '2',
        resourceName: '15楼',
        bindingTime: '2024-01-12T09:15:00Z',
        bindingUser: 'admin',
      },
      metrics: {
        totalExecutions: 1567,
        successRate: 99.9,
        averageResponseTime: 15,
        lastExecution: '2024-01-15T23:45:00Z',
      },
      createdAt: '2024-01-01T00:00:00Z',
      updatedAt: '2024-01-13T12:40:00Z',
    },
  ];

  // 过滤逻辑
  let filteredInstances = capabilityInstances;

  if (query.keyword) {
    const keyword = query.keyword as string;
    filteredInstances = filteredInstances.filter(
      (instance) =>
        instance.name.includes(keyword) ||
        instance.description.includes(keyword) ||
        instance.code.includes(keyword),
    );
  }

  if (query.type) {
    filteredInstances = filteredInstances.filter(
      (instance) => instance.type === query.type,
    );
  }

  if (query.category) {
    filteredInstances = filteredInstances.filter(
      (instance) => instance.category === query.category,
    );
  }

  if (query.resourceType) {
    filteredInstances = filteredInstances.filter(
      (instance) => instance.resourceType === query.resourceType,
    );
  }

  if (query.capabilityType) {
    filteredInstances = filteredInstances.filter(
      (instance) => instance.capabilityType === query.capabilityType,
    );
  }

  if (query.status) {
    filteredInstances = filteredInstances.filter(
      (instance) => instance.status === query.status,
    );
  }

  // 分页
  const page = Number.parseInt(query.page as string) || 1;
  const pageSize = Number.parseInt(query.pageSize as string) || 10;
  const start = (page - 1) * pageSize;
  const end = start + pageSize;
  const items = filteredInstances.slice(start, end);

  return {
    code: 0,
    data: {
      items,
      total: filteredInstances.length,
      page,
      pageSize,
    },
    msg: '获取能力实例列表成功',
  };
});
